package com.springcloud.gateway.provider;

import org.springframework.cloud.gateway.support.ServerWebExchangeUtils;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.util.MultiValueMap;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.util.UriComponentsBuilder;

import java.net.URI;
import java.util.LinkedHashSet;

/**
 * @version V1.0
 * @Author 宋聚智
 * @Date 2019/11/1 8:45
 * @description
 * @Modify By：
 * @ModifyTime： 2019/11/1
 * @Modify marker：
 **/
public class RequestProvider {
    public static String getOriginalRequesetUrl(ServerWebExchange exchange){
        ServerHttpRequest request= exchange.getRequest();
        LinkedHashSet<URI> uris= exchange.getRequiredAttribute(ServerWebExchangeUtils.GATEWAY_ORIGINAL_REQUEST_URL_ATTR);
        URI requestUri=uris.stream().findFirst().orElse(request.getURI());
        MultiValueMap<String,String> queryParams= request.getQueryParams();
        return UriComponentsBuilder.fromPath(requestUri.getRawPath()).queryParams(queryParams).build().toUriString();
    }
}
